flatMap Method

In Scala, flatMap() method is identical to the map() method, but the only difference is that in flatMap the inner grouping of an item is removed and a sequence is generated. It can be defined as a blend of map method and flatten method. The output obtained by running the map method followed by the flatten method is same as obtained by the flatMap(). So, we can say that flatMap first runs the map method and then the flatten method to generate the desired result.

Let’s see an example to illustrate, how the flatMap is working.

object demo
{
  def main(args :Array[String])
   {
      val list = List("Sunday","Monday","Thursday")
      val result = list.flatMap(x => x)
      println(result)
      }  
 }


 map     flatMap
 object demo
{
  def main(args :Array[String])
   {
      val list = List("Sunday","Monday","Thursday")
      var result=list.map(_.toLowerCase).flatten
          println(result)
      }   
 }
object demo
{
  def main(args :Array[String])
   {
      val list = List("Sunday","Monday","Thursday")
      var result=list.flatMap(_.toLowerCase)
          println(result)
      }  
 }
List(s, u, n, d, a, y, m, o, n, d, a, y, t, h, u, r, s, d, a, y)List(s, u, n, d, a, y, m, o, n, d, a, y, t, h, u, r, s, d, a, y)

So, we can see here that the output obtained in both the cases is same therefore, we can say that flatMap is a combination of map and flatten method.


  object Demo
    {
        def main(args:Array[String])
        {
            val days = Seq("sunday", "Monday", "Tuesday")
            val result = days .flatMap(_.toUpperCase)
            println(result)
        }
    }



Here, flatMap is applied to the stated sequence so, a list of sequence of characters is generated.


 object flatmap
    { 
        def main(args:Array[String])
        {
            val list = List(2, 3, 4)
            def func(x:Int) = List(x-1, x, x+1)
            val result = list.flatMap(y => func(y))
            println(result)
           }
    }

List(1, 2, 3, 2, 3, 4, 3, 4, 5)

Here, flatMap is applied on the another function defined in the program and so a list of sequence of numbers is generated. Let’s see how the output is computed.

List(List(2-1, 2, 2+1), List(3-1, 3, 3+1), List(4-1, 4, 4+1))

List(List(1, 2, 3), List(2, 3, 4), List(3, 4, 5))
So, first step works like applying map method on the another function stated.

List(1, 2, 3, 2, 3, 4, 3, 4, 5)
The second step works like applying flatten to the output obtained by the map method at the first step.

object demo
    { 

        def main(args:Array[String])
        {
            val seq = Seq(4, 5, 6, 7)
            val result = seq flatMap { s =>
                            Seq(s, s-1)
            }
            println(result)    
        }
    }

Output:
List(4, 3, 5, 4, 6, 5, 7, 6)

Here, also output is obtained like the first example of the flatMap with another functions.

Utilizing flatMap on if-else statements.

   object Demo
    { 
        def main(args:Array[String])
        {
            val seq = Seq(8, 15, 22, 23, 24)
            val result = seq flatMap { s =>
                    if (s % 3 == 0) Seq(s)
                    else Seq(-s)
        }
              println(result)
        }
    }

To Find the average usong flatMap
val inputrdd = sc.parallelize(Seq(("maths", 50), ("maths", 60), ("english", 65)))
val mapped = inputrdd.mapValues(mark => (mark, 1));
val reduced = mapped.reduceByKey((x, y) => (x._1 + y._1, x._2 + y._2))
val average = reduced.map { x =>
                           val temp = x._2
                           val total = temp._1
                           val count = temp._2
                           (x._1, total / count)
                           }
average.collect

flatten
flatten collapses one level of nested structure.
object Demo {
    def main(args: Array[String]){
    val myList = List(List(1,2), List(3))
    println(myList.flatten)
  }

No comments:

Post a Comment